package com.lambda.hybrid.handler;

import com.lambda.hybrid.Callback;
import com.lambda.hybrid.utils.ACache;

import org.json.JSONObject;

public class SetCacheHandler extends AbstractHandler {
    private final ACache mCache;

    public SetCacheHandler(ACache cache) {
        mCache = cache;
    }

    @Override
    public void onHandle(String data, Callback callback) throws Exception {
        JSONObject table = new JSONObject(data);
        final String key = table.getString("key");
        final String value = table.getString("value");
        int expires = table.optInt("expires");
        if (expires > 0) {
            mCache.put(key, value, expires);
        } else {
            mCache.put(key, value);
        }
    }
}
